package com.cskaoyan.config;

import com.cskaoyan.converter.String2DateConverter;
import com.cskaoyan.interceptor.CustomInterceptor1;
import com.cskaoyan.interceptor.CustomInterceptor2;
import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.format.FormatterRegistry;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import javax.annotation.PostConstruct;
import java.util.Locale;

@EnableWebMvc
@ComponentScan("com.cskaoyan.controller")
public class MvcConfiguration implements WebMvcConfigurer {
    //相关配置都通过WebMvcConfigurer接口来完成
    //可以通过@Bean来注册对应的组件
    //mvc:interceptors
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //默认作用范围就是全局
        registry.addInterceptor(new CustomInterceptor1());
        registry.addInterceptor(new CustomInterceptor2()).addPathPatterns("/hello/**");
    }
    //mvc:resources
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //mvc:resources            mapping                                      location
        registry.addResourceHandler("/pic1/**").addResourceLocations("/");
        registry.addResourceHandler("/pic2/**").addResourceLocations("classpath:/");
        registry.addResourceHandler("/pic3/**").addResourceLocations("file:d:/spring/");
    }

    /*@Autowired
    ConfigurableConversionService conversionService;

    @PostConstruct
    public void addConverter() {
        conversionService.addConverter(new String2DateConverter());
    }

    @Bean
    @Primary
    public ConfigurableConversionService conversionService() {
        return conversionService;
    }*/

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new String2DateConverter());
    }

    @Override
    public Validator getValidator() {
        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.setProviderClass(HibernateValidator.class);
        return localValidatorFactoryBean;
    }

    //注意组件id为固定值
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        return new CommonsMultipartResolver();
    }

    //注意组件id为固定值
    @Bean
    public LocaleResolver localeResolver() {
        CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
        cookieLocaleResolver.setCookieName("language");
        cookieLocaleResolver.setDefaultLocale(Locale.CHINA);
        return cookieLocaleResolver;
    }
}
